package logic

import (
	"Junior_two/subject/usercenter/userapi/internal/utils"
	"context"
	"github.com/astaxie/beego/validation"

	"Junior_two/subject/usercenter/userapi/internal/svc"
	"Junior_two/subject/usercenter/userapi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SendSmsLogic) SendSms(req *types.SenRequest) (resp *types.SenResponse, err error) {
	// todo: add your logic here and delete this line、
	mobile := req.Mobile
	valid := validation.Validation{}
	valid.Required(mobile, "mobile").Message("手机号不可以为空")
	valid.Mobile(mobile, "mobile").Message("手机号个胡思不正确")
	if valid.HasErrors() {
		for _, v := range valid.Errors {
			return &types.SenResponse{
				Code: 201,
				Msg:  v.Message,
			}, nil
		}
	}
	code := utils.Code()
	key := "sms:code_" + mobile
	if !utils.SetSms(key, code) {
		return &types.SenResponse{
			Code: 201,
			Msg:  "短信发送失败",
		}, nil
	}
	return &types.SenResponse{
		Code: 200,
		Msg:  "成功",
		Data: code,
	}, nil
}
